package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class OpProcessos {

	private JTextField tfNomeSo;
	private JTable tblProc;
	private Process p;
	private DefaultTableModel modeloTable;

	public OpProcessos(JTextField tfNomeSo, JTable tblProc) {
		this.tfNomeSo = tfNomeSo;
		this.tblProc = tblProc;
	}

	public String verificaSO() {
		tfNomeSo.setText(System.getProperty("os.name"));
		return System.getProperty("os.name");
	}

	public void listaProcessos() {
		if (System.getProperty("os.name").contains("indows")) {
			try {

				p = Runtime.getRuntime().exec("tasklist.exe /FO TABLE");
				InputStream inputStream = p.getInputStream();
				InputStreamReader inputStreamReader = new InputStreamReader(
						inputStream);
				BufferedReader bufferedReader = new BufferedReader(
						inputStreamReader);
				String linha = bufferedReader.readLine();
				linha = bufferedReader.readLine();

				modeloTable = (DefaultTableModel) tblProc.getModel();
				modeloTable.setRowCount(0);
				modeloTable.setColumnIdentifiers(new Object[] { linha });

				linha = bufferedReader.readLine();

				while (linha != null) {

					linha = linha.replace("=", " ");

					if (!linha.trim().isEmpty()) {
						modeloTable.addRow(new Object[] { linha });
					}

					linha = bufferedReader.readLine();
				}

				bufferedReader.close();
				inputStreamReader.close();
				inputStream.close();
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, e.getMessage(), "Erro",
						JOptionPane.ERROR_MESSAGE);
			}
		} else {
			if (System.getProperty("os.name").contains("inux")) {
				try {
					p = Runtime.getRuntime().exec("ps -ef");
					InputStream inputStream = p.getInputStream();
					InputStreamReader inputStreamReader = new InputStreamReader(
							inputStream);
					BufferedReader bufferedReader = new BufferedReader(
							inputStreamReader);
					String linha = bufferedReader.readLine();
					
					modeloTable = (DefaultTableModel) tblProc.getModel();
					modeloTable.setRowCount(0);
					modeloTable.setColumnIdentifiers(new Object[] { linha });

					linha = bufferedReader.readLine();
					
					while (linha != null) {
						
						if (!linha.trim().isEmpty()) {
							modeloTable.addRow(new Object[] { linha });
						}
						
						linha = bufferedReader.readLine();
					}
					bufferedReader.close();
					inputStreamReader.close();
					inputStream.close();
				} catch (IOException e) {
					JOptionPane.showMessageDialog(null, e.getMessage(), "Erro",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}

	public void mataProcesso() {
		int numLinha = tblProc.getSelectedRow();
		String linha = tblProc.getValueAt(numLinha,tblProc.getSelectedColumn()).toString(); 
		if (System.getProperty("os.name").contains("indows")) {
			String[] proc = linha.split("  ");
			int dialogButton = JOptionPane.YES_NO_OPTION;
			JOptionPane.showConfirmDialog(null,
					"Deseja tentar matar o processo "
							+ proc[0] + " ?",
					"Matar Processo", JOptionPane.WARNING_MESSAGE);
			if (dialogButton == JOptionPane.YES_OPTION) {
				try {
					Runtime.getRuntime().exec(
							"taskkill /IM "
									+ proc[0]);

				} catch (IOException e) {
					JOptionPane.showMessageDialog(null, e.getMessage(), "Erro",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		} else {
			if (System.getProperty("os.name").contains("inux")) {
				String pid = "";
				for (int i = 0 ; i < linha.length() ; i++){
					char letra = linha.charAt(i);
					if (Character.isDigit(letra)){
						pid = pid + letra;
						if (linha.charAt(i + 1) == " ".charAt(0)){
							break;
						}
					}
				}
				int dialogButton = JOptionPane.YES_NO_OPTION;
				JOptionPane.showConfirmDialog(null,
						"Deseja tentar matar o processo PID = "+
								pid +  " ?",
						"Matar Processo", JOptionPane.WARNING_MESSAGE);
				if (dialogButton == JOptionPane.YES_OPTION) {
					try {
						Runtime.getRuntime().exec(
								"kill -9 " + pid);
					} catch (IOException e) {
						JOptionPane.showMessageDialog(null, e.getMessage(),
								"Erro", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		}
		modeloTable.removeRow(numLinha);
		tblProc.repaint();
	}

	public void atualizarListas() {
		// taProcessos.setText("");
		// cbProc.removeAllItems();
		modeloTable.setRowCount(0);
		listaProcessos();
	}

}
